package com.whnk.attendance.service.impl;

import java.util.List;

import com.whnk.attendance.vo.*;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.attendance.mapper.ProjectClassifyInfoMapper;
import com.whnk.attendance.domain.ProjectClassifyInfo;
import com.whnk.attendance.service.IProjectClassifyInfoService;

/**
 * 项目分类列Service业务层处理
 * 
 * @author whnk
 * @date 2021-12-14
 */
@Service
public class ProjectClassifyInfoServiceImpl implements IProjectClassifyInfoService 
{
    @Autowired
    private ProjectClassifyInfoMapper projectClassifyInfoMapper;

    /**
     * 查询项目分类列表
     *
     * @param proClaId 项目分类列表主键
     * @return 项目分类列表
     */
    @Override
    public ProjectClassifyInfo selectProjectClassifyInfoByProClaId(Long proClaId)
    {
        return projectClassifyInfoMapper.selectProjectClassifyInfoByProClaId(proClaId);
    }

    /**
     * 查询项目分类列表列表
     *
     * @param projectClassifyInfo 项目分类列表
     * @return 项目分类列表
     */
    @Override
    public List<ProjectClassifyInfo> selectProjectClassifyInfoList(ProjectClassifyInfo projectClassifyInfo)
    {
        return projectClassifyInfoMapper.selectProjectClassifyInfoList(projectClassifyInfo);
    }

    /**
     * 新增项目分类列表
     *
     * @param projectClassifyInfo 项目分类列表
     * @return 结果
     */
    @Override
    public int insertProjectClassifyInfo(ProjectClassifyInfo projectClassifyInfo)
    {
        projectClassifyInfo.setCreateTime(DateUtils.getNowDate());
        return projectClassifyInfoMapper.insertProjectClassifyInfo(projectClassifyInfo);
    }

    /**
     * 修改项目分类列表
     *
     * @param projectClassifyInfo 项目分类列表
     * @return 结果
     */
    @Override
    public int updateProjectClassifyInfo(ProjectClassifyInfo projectClassifyInfo)
    {
        projectClassifyInfo.setUpdateTime(DateUtils.getNowDate());
        return projectClassifyInfoMapper.updateProjectClassifyInfo(projectClassifyInfo);
    }

    /**
     * 批量删除项目分类列表
     *
     * @param proClaIds 需要删除的项目分类列表主键
     * @return 结果
     */
    @Override
    public int deleteProjectClassifyInfoByProClaIds(Long[] proClaIds)
    {
        return projectClassifyInfoMapper.deleteProjectClassifyInfoByProClaIds(proClaIds);
    }

    /**
     * 删除项目分类列表信息
     *
     * @param proClaId 项目分类列表主键
     * @return 结果
     */
    @Override
    public int deleteProjectClassifyInfoByProClaId(Long proClaId)
    {
        return projectClassifyInfoMapper.deleteProjectClassifyInfoByProClaId(proClaId);
    }

    /*
     * 分页查询项目分类列表
     */
    public List<ProjectClassifyInfoListVo> selectProjectClassifyInfoListVoList(ProjectClassifyInfoQueryVo projectClassifyInfoQueryVo)
    {
        return projectClassifyInfoMapper.selectProjectClassifyInfoListVoList(projectClassifyInfoQueryVo);
    }

    /*
     * 根据逻辑主键查询项目分类信息
     */
    public ProjectClassifyInfoListVo selectProjectClassifyInfoListVoByProClaId(Long proClaId)
    {
        return projectClassifyInfoMapper.selectProjectClassifyInfoListVoByProClaId(proClaId);
    }

//     * 新增修改界面交付区域下拉框
//
//
//    public List<ProjectClassifyInfoAreaVo> areaInfoList(){
//        return projectClassifyInfoMapper.areaInfoList();
//    }
//
//     * 新增修改界面社保分类编号下拉框
//
//
//    public List<ProjectClassifyInfoSocialInsNumVo> socialInsNumList(){
//        return projectClassifyInfoMapper.socialInsNumList();
//    }

    /*
     * 根据项目分类编号查询项目分类信息
     */
    public ProjectClassifyInfoListVo selectProjectClassifyInfoListVoByProClaNum(String proClaNum){
        return projectClassifyInfoMapper.selectProjectClassifyInfoListVoByProClaNum(proClaNum);
    }

    /**
     * 校验项目分类编号是否唯一
     */
    public boolean checkProClaNum(ProjectClassifyInfoUpdateVo projectClassifyInfoUpdateVo){
        ProjectClassifyInfoListVo info = projectClassifyInfoMapper.checkProClaNum(projectClassifyInfoUpdateVo.getProClaNum());
        if(StringUtils.isNotNull(info) && info.getProClaId() != projectClassifyInfoUpdateVo.getProClaId()){
            return false;
        }
        return true;
    }

    /*
     * 新增项目分类信息
     */
    public int insertProjectClassifyInfo(ProjectClassifyInfoUpdateVo projectClassifyInfoUpdateVo){
        ProjectClassifyInfo projectClassifyInfo = new ProjectClassifyInfo();
        projectClassifyInfo.setProClaId(projectClassifyInfoUpdateVo.getProClaId());
        projectClassifyInfo.setProClaNum(projectClassifyInfoUpdateVo.getProClaNum());
        projectClassifyInfo.setProClaName(projectClassifyInfoUpdateVo.getProClaName());
        projectClassifyInfo.setAreaId(projectClassifyInfoUpdateVo.getAreaId());
        projectClassifyInfo.setPublicCost(projectClassifyInfoUpdateVo.getPublicCost());
        projectClassifyInfo.setSocialInsNum(projectClassifyInfoUpdateVo.getSocialInsNum());
        projectClassifyInfo.setCreateTime(DateUtils.getNowDate());
        return projectClassifyInfoMapper.insertProjectClassifyInfo(projectClassifyInfo);
    }

    /**
     * 修改项目分类信息
     */
    public int updateProjectClassifyInfo(ProjectClassifyInfoUpdateVo projectClassifyInfoUpdateVo){
        ProjectClassifyInfo projectClassifyInfo = new ProjectClassifyInfo();
        projectClassifyInfo.setProClaId(projectClassifyInfoUpdateVo.getProClaId());
        projectClassifyInfo.setProClaNum(projectClassifyInfoUpdateVo.getProClaNum());
        projectClassifyInfo.setProClaName(projectClassifyInfoUpdateVo.getProClaName());
        projectClassifyInfo.setAreaId(projectClassifyInfoUpdateVo.getAreaId());
        projectClassifyInfo.setPublicCost(projectClassifyInfoUpdateVo.getPublicCost());
        projectClassifyInfo.setSocialInsNum(projectClassifyInfoUpdateVo.getSocialInsNum());
        projectClassifyInfo.setUpdateTime(DateUtils.getNowDate());
        return projectClassifyInfoMapper.updateProjectClassifyInfo(projectClassifyInfo);
    }

    /*
     * 导入项目分类信息
     */
    public String importData(List<ProjectClassifyInfoListVo> projectClassifyInfoList, Boolean isUpdateSupport){
        if (StringUtils.isNull(projectClassifyInfoList) || projectClassifyInfoList.size() == 0)
        {
            throw new ServiceException("导入项目分类信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ProjectClassifyInfoListVo projectClassifyInfoListVo : projectClassifyInfoList)
        {
            try
            {
                // 验证是否存在这个项目分类信息
                ProjectClassifyInfoListVo projectClassify = projectClassifyInfoMapper.selectProjectClassifyInfoListVoByProClaNum(projectClassifyInfoListVo.getProClaNum());
                if (StringUtils.isNull(projectClassify))
                {
                    ProjectClassifyInfoUpdateVo projectClassifyInfoUpdateVo = new ProjectClassifyInfoUpdateVo();
                    projectClassifyInfoUpdateVo.setProClaNum(projectClassifyInfoListVo.getProClaNum());
                    projectClassifyInfoUpdateVo.setProClaName(projectClassifyInfoListVo.getProClaName());
                    projectClassifyInfoUpdateVo.setAreaId(projectClassifyInfoListVo.getAreaId());
                    projectClassifyInfoUpdateVo.setSocialInsNum(projectClassifyInfoListVo.getSocialInsNum());
                    projectClassifyInfoUpdateVo.setPublicCost(projectClassifyInfoListVo.getPublicCost());
                    this.insertProjectClassifyInfo(projectClassifyInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目分类信息 " + projectClassifyInfoListVo.getProClaNum() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    ProjectClassifyInfoUpdateVo projectClassifyInfoUpdateVo = new ProjectClassifyInfoUpdateVo();
                    //为修改对象的项目分类主键赋值
                    projectClassifyInfoUpdateVo.setProClaId(projectClassify.getProClaId());
                    projectClassifyInfoUpdateVo.setProClaNum(projectClassifyInfoListVo.getProClaNum());
                    projectClassifyInfoUpdateVo.setProClaName(projectClassifyInfoListVo.getProClaName());
                    projectClassifyInfoUpdateVo.setAreaId(projectClassifyInfoListVo.getAreaId());
                    projectClassifyInfoUpdateVo.setSocialInsNum(projectClassifyInfoListVo.getSocialInsNum());
                    projectClassifyInfoUpdateVo.setPublicCost(projectClassifyInfoListVo.getPublicCost());
                    this.updateProjectClassifyInfo(projectClassifyInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目分类信息 " + projectClassifyInfoListVo.getProClaNum() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、项目分类信息 " + projectClassifyInfoListVo.getProClaNum() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目分类信息 " + projectClassifyInfoListVo.getProClaNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /*
     * 项目分类编号id，项目分类名称name接口
     */
    public List<ProjectClassifyListVo> projectClassifyList(){
        return projectClassifyInfoMapper.projectClassifyList();
    }
}
